A full inforuction to the project and a description of Kallisto alignment and data filtering/normalization steps can be found in Ss_RNAseq_Data_Preprocessing.rmd.
Principal component analysis applied to filtered and TMM-normalized expression data identified two developmental clusters that account for 42.6% and 28.9% of expression variability between S. stercoralis developmental stages.
The limma package ( Ritchie et al 2015, Phipson et al 2016) is used to conduct pairwise differential gene expression analyses between life stages. An example pairwise comparisons between two life stages is displayed as a volcano plot and interactive DataTable.
All code is echoed under descriptive headers; code chunks are hidden from view by default. Users may show hidden R code by clicking the Show buttons. In addition, all code chunks are collated at the end of the document in an Appendix.
This code loads R data objects that has been preproprocessed by Ss_RNAseq_Data_Preprocessing.rmd.
load (file = "../Outputs/SsRNAseq_data_preprocessed")
targets <- SsRNAseq.preprocessed.data$targets
annotations <- SsRNAseq.preprocessed.data$annotations
log2.cpm.filtered.norm <- SsRNAseq.preprocessed.data$log2.cpm.filtered.norm
myDGEList.filtered.norm <-SsRNAseq.preprocessed.data$myDGEList.filtered.norm
rm(SsRNAseq.preprocessed.data)
load(file = "../Outputs/Ss_vDGEList")
# Check for presence of output plots folder, generate if it doesn't exist
output.path <- "../Outputs/Plots"
if (!dir.exists(output.path)){
dir.create(output.path)
}
This code chunk starts with filtered and normalized abundance data in a data frame (not tidy). It will implement hierarchical clustering and PCA analyses on the data. It will plot various graphs, including a dendrogram of the heirachical clustering, and several plots of visualize the PCA.
# Introduction to this chunk -----------
# This code chunk starts with filtered and normalized abundance data in a data frame (not tidy).
# It will implement hierarchical clustering and PCA analyses on the data.
# It will plot various graphs and can save them in PDF files.
# Load packages ------
suppressPackageStartupMessages({
library(tidyverse) # you're familiar with this fromt the past two lectures
library(ggplot2)
library(RColorBrewer)
library(ggdendro)
library(magrittr)
library(factoextra)
library(gridExtra)
library(cowplot)
library(dendextend)
})
# Identify variables of interest in study design file ----
group <- factor(targets$group)
# Hierarchical clustering ---------------
# Remember: hierarchical clustering can only work on a data matrix, not a data frame
# Calculate distance matrix
# dist calculates distance between rows, so transpose data so that we get distance between samples.
# how similar are samples from each other
colnames(log2.cpm.filtered.norm)<-targets$group
distance <- dist(t(log2.cpm.filtered.norm), method = "maximum") #other distance methods are "euclidean", maximum", "manhattan", "canberra", "binary" or "minkowski"
# Calculate clusters to visualize differences. This is the hierarchical clustering.
# The methods here include: single (i.e. "friends-of-friends"), complete (i.e. complete linkage), and average (i.e. UPGMA). Here's a comparison of different types: https://en.wikipedia.org/wiki/UPGMA#Comparison_with_other_linkages
clusters <- hclust(distance, method = "complete") #other agglomeration methods are "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", or "centroid"
dend <- as.dendrogram(clusters)
p1<-dend %>%
dendextend::set("branches_k_color", k = 5) %>%
dendextend::set("hang_leaves", c(0.1)) %>%
dendextend::set("labels_cex", c(0.5)) %>%
dendextend::set("labels_colors", k = 5) %>%
dendextend::set("branches_lwd", c(0.7)) %>%
as.ggdend %>%
ggplot (offset_labels = -0.5) +
theme_dendro() +
ylim(0, max(get_branches_heights(dend))) +
labs(title = "Hierarchical Cluster Dendrogram ",
subtitle = "filtered, TMM normalized",
y = "Distance",
x = "Life stage") +
coord_fixed(1/2) +
theme(axis.title.x = element_text(color = "black"),
axis.title.y = element_text(angle = 90),
axis.text.y = element_text(angle = 0),
axis.line.y = element_line(color = "black"),
axis.ticks.y = element_line(color = "black"),
axis.ticks.length.y = unit(2, "mm"))
Clustering performed on filtered and normalized abundance data using the “complete” method.
# Principal component analysis (PCA) -------------
# this also works on a data matrix, not a data frame
pca.res <- prcomp(t(log2.cpm.filtered.norm), scale.=F, retx=T)
summary(pca.res) # Prints variance summary for all principal components.
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 124.8775 105.7601 60.28878 53.37388 41.40321 26.13575
## Proportion of Variance 0.4219 0.3026 0.09833 0.07707 0.04638 0.01848
## Cumulative Proportion 0.4219 0.7245 0.82282 0.89989 0.94627 0.96475
## PC7 PC8 PC9 PC10 PC11 PC12
## Standard deviation 20.28184 13.66342 10.37244 9.93966 9.00105 8.75746
## Proportion of Variance 0.01113 0.00505 0.00291 0.00267 0.00219 0.00207
## Cumulative Proportion 0.97588 0.98093 0.98384 0.98651 0.98870 0.99078
## PC13 PC14 PC15 PC16 PC17 PC18 PC19
## Standard deviation 7.89616 7.37748 7.04007 6.52221 6.41971 6.00467 5.58038
## Proportion of Variance 0.00169 0.00147 0.00134 0.00115 0.00111 0.00098 0.00084
## Cumulative Proportion 0.99246 0.99394 0.99528 0.99643 0.99754 0.99852 0.99936
## PC20 PC21
## Standard deviation 4.85766 6.462e-14
## Proportion of Variance 0.00064 0.000e+00
## Cumulative Proportion 1.00000 1.000e+00
#pca.res$rotation #$rotation shows you how much each gene influenced each PC (called 'scores')
#pca.res$x # 'x' shows you how much each sample influenced each PC (called 'loadings')
#note that these have a magnitude and a direction (this is the basis for making a PCA plot)
## This generates a screeplot: a standard way to view eigenvalues for each PCA. Shows the proportion of variance accounted for by each PC. Plotting only the first 10 dimensions.
p2<-fviz_eig(pca.res,
barcolor = brewer.pal(8,"Pastel2")[8],
barfill = brewer.pal(8,"Pastel2")[8],
linecolor = "black",
main = "Scree plot: proportion of variance accounted for by each principal component",
ggtheme = theme_bw())
A scree plot is a standard way to view eigenvalues for each PCA. The plot shows the proportion of variance accounted for by each PC.
p2
pc.var<-pca.res$sdev^2 # sdev^2 captures these eigenvalues from the PCA result
pc.per<-round(pc.var/sum(pc.var)*100, 1) # we can then use these eigenvalues to calculate the percentage variance explained by each PC
# Visualize the PCA result ------------------
#lets first plot any two PCs against each other
#We know how much each sample contributes to each PC (loadings), so let's plot
pca.res.df <- as_tibble(pca.res$x)
# Plotting PC1 and PC2
p3<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
fill = targets$group,
color = targets$group
) +
geom_point(size=4, shape= 21, color = "black", alpha = 0.5) +
scale_fill_brewer(palette = "Set2") +
scale_color_brewer(palette = "Set2", guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(title="Principal Components Analysis of S. stercoralis RNAseq Samples",
sub = "Note: analysis is blind to life stage identity.",
fill = "Life Stage") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10))
suppressMessages(ggsave("Ss_Multivariate_Plots_PCA.pdf",
plot = p3,
device = "pdf",
height = 4,
#width = 7,
path = output.path))
Plot of the samples in PCA space. Fill color indicates life stage.
# A guess at the identity of PC1
p4<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Maturity),
fill = factor(targets$Maturity)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")) +
scale_color_manual(values = brewer.pal(8,"Set2"), guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC1 identity: Adults vs Larvae",
fill = "Maturity") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
Plot of the samples in PCA space. Fill color corresponds to potential identity.
# A guess at the identity of PC2
p5<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Infectious),
fill = factor(targets$Infectious)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")[3:4]) +
scale_color_manual(values = brewer.pal(8,"Set2")[3:4], guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC2 identity: Host-seeking/dwelling",
fill = 'Infectivity Stage') +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
Plot of the samples in PCA space. Fill color corresponds to potential identity.
# Create a PCA 'small multiples' chart ----
pca.res.df <- pca.res$x[,1:6] %>%
as_tibble() %>%
add_column(sample = targets$sample,
group = group,
maturity = factor(targets$Maturity),
infectious = factor(targets$Infectious))
pca.pivot <- pivot_longer(pca.res.df, # dataframe to be pivoted
cols = PC1:PC3, # column names to be stored as a SINGLE variable
names_to = "PC", # name of that new variable (column)
values_to = "loadings") # name of new variable (column) storing all the values (data)
PC1<-subset(pca.pivot, PC == "PC1")
PC2 <-subset(pca.pivot, PC == "PC2")
PC3 <- subset(pca.pivot, PC == "PC3")
#PC4 <- subset(pca.pivot, PC == "PC4")
# New facet label names for PCs
PC.labs <- c(paste0("PC1 (",pc.per[1],"%",")"),
paste0("PC2 (",pc.per[2],"%",")"),
paste0("PC3 (",pc.per[3],"%",")")
)
names(PC.labs) <- c("PC1", "PC2", "PC3")
p6<-ggplot(pca.pivot) +
aes(x=sample, y=loadings) + # you could iteratively 'paint' different covariates onto this plot using the 'fill' aes
geom_bar(stat="identity", fill = brewer.pal(8,"Set2")[8]) +
scale_fill_brewer(palette = "Set2") +
facet_wrap(~PC, labeller = labeller(PC = PC.labs)) +
geom_bar(data = PC1, stat = "identity", aes(fill = group)) +
geom_bar(data = PC2, stat = "identity", aes(fill = group)) +
geom_bar(data = PC3, stat = "identity", aes(fill = group)) +
labs(title="S. stercoralis: PCA 'small multiples' plot",
fill = "Life Stage",
caption=paste0("produced on ", Sys.time())) +
scale_x_discrete(limits = targets$sample, labels = targets$sample) +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10)) +
coord_flip()
suppressMessages(ggsave("Ss_Multivariate_Plots_Small_Multiples.pdf",
plot = p6,
device = "pdf",
height = 4,
width = 8,
path = output.path))
This chunk provides additional analysis of the principal components, in order to determine which genes are influencing the identified PCs. It prints an annotated list of genes that are the 10% of contributors (in any direction) to PC1 and PC2.
# Introduction to this chunk ----
# This chunk provides additional analysis of the principal components, in order to determine which genes are influencing the identified PCs.
# Use pca.res$rotation to select genes influencing PC1-6 ----
myscores.df <- pca.res$rotation[,1:6] %>%
as_tibble(rownames = "geneID") %>%
pivot_longer(cols = -geneID, names_to = "PC", values_to = "scores") %>%
dplyr::mutate(abs_scores = abs(scores)) %>%
group_by(PC) %>%
slice_max(abs_scores, prop = .1) # get top 10% of genes in all PCs
# Pull out genes that are the top 10% of contributors (in any direction) to PC1 and PC2. Annotate.
myscores.Top10 <- myscores.df %>%
dplyr::filter(PC == "PC1" | PC == "PC2") %>%
pivot_wider(id_cols = geneID,
names_from = PC,
values_from = scores) %>%
dplyr::mutate(PC1_id = case_when(PC1 > 0 ~ "larval",
PC1 < 0 ~ "adult",
is.na(PC1) ~ "--")) %>%
dplyr::mutate(PC2_id = case_when(PC2 > 0 ~ "parasite",
PC2 < 0 ~ "non_parasite",
is.na(PC2) ~ "--")) %>%
dplyr::left_join(.,(rownames_to_column(annotations, var = "geneID")), by = "geneID") %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term, Ce_geneID, Ce_percent_homology, .after = PC2_id)
# Make Interactive Plot
myscores.Top10.interactive <- myscores.Top10 %>%
DT::datatable(extensions = c('KeyTable', "FixedHeader"),
rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left;',
htmltools::tags$b('Top 10% of Genes Contributing to PC1 and PC2'),
htmltools::tags$br(),
'Search for PC1_id/PC2_id values to filter results'),
options = list(keys = TRUE,
autoWidth = TRUE,
scrollX = TRUE,
scrollY = '300px',
order = list(1, 'desc'),
searchHighlight = TRUE,
pageLength = 10,
lengthMenu = c("10", "25", "50", "100"))) %>%
DT::formatRound(columns=c(2:3), digits=3)
myscores.Top10.interactive
Make a heatmap for all the genes using the Log2CPM values.
suppressPackageStartupMessages({
library(pheatmap)
library(RColorBrewer)
library(heatmaply)
})
diffGenes <- v.DEGList.filtered.norm$E %>%
as_tibble(rownames = "geneID", .name_repair = "unique") %>%
dplyr::select(!geneID) %>%
as.matrix()
## Loading required package: limma
rownames(diffGenes) <- rownames(v.DEGList.filtered.norm$E)
colnames(diffGenes) <- as.character(v.DEGList.filtered.norm$targets$samples)
clustColumns <- hclust(as.dist(1-cor(diffGenes, method="spearman")), method="complete")
clustRows <- hclust(as.dist(1-cor(t(diffGenes),
method="pearson")),
method="complete")
par(cex.main=0.5)
showticklabels <- c(TRUE,FALSE)
p<-pheatmap(diffGenes,
color = RdBu(75),
cluster_rows = clustRows,
cluster_cols = clustColumns,
show_rownames = F,
scale = "row",
angle_col = 45,
main = "Ss: Log2 Counts Per Million (CPM) Expression Across Life Stages"
)
# suppressMessages(ggsave('Ss_Log2CPM_Heatmap.png',
# plot = p,
# width = 6,
# height = 4,
# device = "png",
# #useDingbats=FALSE,
# path = output.path))
#
# p
This chunk uses a variance-stabilized DGEList of filtered and normalized abundance data. These data/results are examples, a responsive version of this code is avaliable in a Shiny App.
# Introduction to this chunk ----
# This chunk uses a variance-stabilized DGEList of filtered and normalized abundance data.
#
# These data/results are examples, a responsive version of this code is avaliable in a Shiny App.
#
# Because we have access to biological replicates, we can use statistical tools for differential expression analysis
# Useful reading on differential expression: https://ucdavis-bioinformatics-training.github.io/2018-June-RNA-Seq-Workshop/thursday/DE.html
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma) # differential gene expression using linear modeling
library(edgeR)
library(gt)
library(DT)
library(plotly)
library(ggthemes)
library(RColorBrewer)
source("./theme_Publication.R")
})
diffGenes.df <- v.DEGList.filtered.norm$E %>%
as_tibble(rownames = "geneID", .name_repair = "unique")
# Set Expression threshold values for plotting and saving DEGs ----
adj.P.thresh <- 0.05
lfc.thresh <- 1
group <- factor(targets$group)
design <- model.matrix(~0 + group) # no intercept/blocking for matrix, comparisons across group
colnames(design) <- levels(group)
# Fit a linear model to the data ----
fit <- lmFit(v.DEGList.filtered.norm, design)
# As an example, generate comparison matrix for a pairwise comparison ----
# iL3s vs FLF
# Note that the target/contrast goups will be divided by the number of life
# stage groups e.g. PF+FLF/2 - iL3+iL3a+pfL1+ppL1+ppL3/5
comparison <- c('(iL3)-(FLF)')
targetStage<- comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,1] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
contrastStage<-comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,2] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
comparison<- sapply(seq_along(comparison),function(x){
tS <- as.vector(targetStage[x,]) %>%
.[. != ""]
cS <- as.vector(contrastStage[x,]) %>%
.[. != ""]
paste(paste0(tS,
collapse = "+") %>%
paste0("(",.,")/",length(tS)),
paste0(cS,
collapse = "+") %>%
paste0("(",.,")/",length(cS)),
sep = "-")
})
# Generate contrast matrix ----
contrast.matrix <- makeContrasts(contrasts = comparison,
levels=design)
# extract the linear model fit -----
fits <- contrasts.fit(fit, contrast.matrix)
# empirical bayes smoothing of gene-wise standard deviations provides increased power (see: https://www.degruyter.com/doi/10.2202/1544-6115.1027)
ebFit <- eBayes(fits)
# Pull out the DEGs that pass a specific threshold for all pairwise comparisons ----
# Adjust for multiple comparisons using method = global.
results <- decideTests(ebFit, method="global", adjust.method="BH", p.value = adj.P.thresh)
recode01<- function(x){
case_when(x == 1 ~ "Up",
x == -1 ~ "Down",
x == 0 ~ "NotSig")
}
diffDesc <- results %>%
as_tibble(rownames = "geneID") %>%
dplyr::mutate(across(-geneID, unclass)) %>%
dplyr::mutate(across(where(is.double), recode01))
# Function that identifies top DEGs between a specific contrast ----
calc_DEG_tbl <- function (ebFit, coef) {
myTopHits.df <- limma::topTable(ebFit, adjust ="BH",
coef=coef, number=40000,
sort.by="logFC") %>%
as_tibble(rownames = "geneID") %>%
dplyr::rename(tStatistic = t, LogOdds = B, BH.adj.P.Val = adj.P.Val) %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term,
In.subclade_geneID, In.subclade_percent_homology,
Out.subclade_geneID, Out.subclade_percent_homology,
Ce_geneID, Ce_percent_homology, .after = LogOdds)
myTopHits.df
}
list.myTopHits.df <- sapply(comparison, function(y){
calc_DEG_tbl(ebFit, y)},
simplify = FALSE,
USE.NAMES = TRUE)
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::select(geneID,
logFC,
BH.adj.P.Val:Ce_percent_homology)},
simplify = FALSE,
USE.NAMES = TRUE)
# Get log2CPM values and threshold information for genes of interest
list.myTopHits.df <- sapply(seq_along(comparison), function(y){
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
concat_name <- function(x) {
ifelse(x == "target",
paste(tS, collapse = "+"),
paste(cS, collapse = "+"))
}
groupAvgs <- diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
pivot_longer(cols = -geneID, names_to = c("group","sample"), values_to = "CPM",
names_sep = "-") %>%
dplyr::mutate(contrastID = if_else(group %in% tS,"target", "contrast")) %>%
group_by(geneID, contrastID) %>%
dplyr::select(-sample) %>%
summarize(mean = mean(CPM), .groups = "drop_last") %>%
pivot_wider(names_from = contrastID, values_from = mean) %>%
dplyr::relocate(contrast, .after = target) %>%
dplyr::rename_with(concat_name, -geneID) %>%
dplyr::rename_with(.cols =-geneID, .fn = ~ paste0("avg_(",.x,")"))
diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
left_join(groupAvgs, by = "geneID") %>%
left_join(list.myTopHits.df[[y]],., by = "geneID") %>%
left_join(dplyr::select(diffDesc,geneID,comparison[y]), by = "geneID") %>%
dplyr::rename(DEG_Desc=comparison[y]) %>%
dplyr::relocate(DEG_Desc) %>%
dplyr::relocate(logFC:Ce_percent_homology, .after = last_col())
},
simplify = FALSE)
comparison <- gsub("/[0-9]*","", comparison)
names(list.myTopHits.df) <- comparison
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::mutate(DEG_Desc = case_when(DEG_Desc == "Up" ~ paste0("Up in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "Down" ~ paste0("Down in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "NotSig" ~ "NotSig"))
},
simplify = FALSE,
USE.NAMES = TRUE)
# PC1 Volcano Plot and Interactive Table ----
vplot1 <- ggplot(list.myTopHits.df[[1]]) +
aes(y=-log10(BH.adj.P.Val), x=logFC, text = paste(geneID, "<br>",
"logFC:", round(logFC, digits = 2), "<br>",
"p-val:", format(BH.adj.P.Val, digits = 3, scientific = TRUE))) +
geom_point(size=2) +
geom_hline(yintercept = -log10(adj.P.thresh),
linetype="longdash",
colour="grey",
size=1) +
geom_vline(xintercept = lfc.thresh,
linetype="longdash",
colour="#BE684D",
size=1) +
geom_vline(xintercept = -lfc.thresh,
linetype="longdash",
colour="#2C467A",
size=1) +
labs(title = paste0('Pairwise Comparison: ',
gsub('-',
' vs ',
comparison[1])),
subtitle = paste0("grey line: p = ",
adj.P.thresh, "; colored lines: log-fold change = ", lfc.thresh),
color = "GeneIDs") +
theme_Publication()
vplot1
# Interactive Tables
y<- 1
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
sample.num.tS <- sapply(tS, function(x) {colSums(v.DEGList.filtered.norm$design)[[x]]}) %>% sum()
sample.num.cS <- sapply(cS, function(x) {colSums(v.DEGList.filtered.norm$design)[[x]]}) %>% sum()
n_num_cols <- sample.num.tS + sample.num.cS + 5
index_homologs <- length(colnames(list.myTopHits.df[[y]])) - 5
LS.datatable <- list.myTopHits.df[[y]] %>%
DT::datatable(rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left; color: black',
htmltools::tags$b('Differentially Expressed Genes in',
htmltools::tags$em('S. stercoralis'),
gsub('-',' vs ',comparison[y])),
htmltools::tags$br(),
"Threshold: p < ",
adj.P.thresh, "; log-fold change > ",
lfc.thresh,
htmltools::tags$br(),
'Values = log2 counts per million'),
options = list(autoWidth = TRUE,
scrollX = TRUE,
scrollY = '300px',
scrollCollapse = TRUE,
order = list(n_num_cols-1,
'desc'),
searchHighlight = TRUE,
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
list(
targets = ((n_num_cols +
1)),
render = JS(
"function(data, row) {",
"data.toExponential(1);",
"}")
),
list(
targets = ((n_num_cols +
4):(n_num_cols +
5)),
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 20 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 20) + '...</span>' : data;",
"}")
),
list(targets = "_all",
class="dt-right")
),
rowCallback = JS(c(
"function(row, data){",
" for(var i=0; i<data.length; i++){",
" if(data[i] === null){",
" $('td:eq('+i+')', row).html('NA')",
" .css({'color': 'rgb(151,151,151)', 'font-style': 'italic'});",
" }",
" }",
"}"
))
))
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(3:n_num_cols),
digits=3)
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(n_num_cols+2,
index_homologs+1,
index_homologs+3),
digits=2)
LS.datatable <- LS.datatable %>%
DT::formatSignif(columns=c(n_num_cols+1),
digits=3)
LS.datatable
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
This section compares potential results to a published analsysis. Here, we use Supplementary Table 5 from Hunt et al 2016, which includes the results of edgeR differential gene expression analysis between multiple life stages.
suppressPackageStartupMessages({
library(openxlsx)
library(tidyverse)
library(ggplot2)
})
# Load Hunt Dataset: iL3 vs FLF comparison
temp.dat <- read.xlsx ("../Benchmarking/ng.3495-S5.xlsx",
sheet = 5, startRow = 2)
Hunt.dat <- tibble(geneID = temp.dat$X2, logFC = temp.dat$logFC)
rm(temp.dat)
# Rename Results of iL3 vs FLF comparison from Browser
Browser.dat <- list.myTopHits.df$`(iL3)-(FLF)` %>%
dplyr::select(geneID, logFC)
print(paste('Total number of genes in Hunt *et al* 2016 iL3 vs FLF comparison tab:',nrow(Hunt.dat)))
## [1] "Total number of genes in Hunt *et al* 2016 iL3 vs FLF comparison tab: 11188"
print(paste('Total number of genes in Str-RNAseq Browser iL3 vs FLF output file:', nrow(Browser.dat)))
## [1] "Total number of genes in Str-RNAseq Browser iL3 vs FLF output file: 12381"
# The plot below takes the genes with LogFC results in both the Browser and Hunt databases, and plots the two sets against each other.
plotting.all <- inner_join(Browser.dat, Hunt.dat, by = "geneID")
p.benchmark <- ggplot(plotting.all, aes(x = logFC.x, y = logFC.y)) +
geom_smooth(method=lm, color = 'red', formula = "y ~ x") +
geom_point(shape=16, size=3, alpha = 0.8) +
labs(title = "S. stercoralis: Str-Browser vs Hunt Data",
subtitle = "iL3 vs FLF",
caption = "points = genes; red line = linear model (y ~ x)",
x = "Str-Browser LogFC",
y = "Hunt LogFC") +
coord_equal() +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10))
suppressMessages(ggsave("Ss_Benchmarking.pdf",
plot = p.benchmark,
device = "pdf",
height = 4,
#width = 8,
path = output.path))
p.benchmark
This code prerform GSEA using the clusterProfiler library. Ability to do this depends on the availability of gene sets. Major databases (e.g. msigdb don’t seem to have Strongyloides information. They do have C. elegans gene sets, but I’m not convinced the homology information is good enough for the comparison to be unbiased/meaningful. In Hunt et al 2016, there is an Ensembl Compara protein family set; we will use this as the basis for our gene set libraries.
Note that this uses specific transcript information, which I throw out (e.g. SSTP_0001137400.2 is recoded as SSTP_0001137400).
Given a priori defined set of gene S (e.g., genes shareing the same DO category), the goal of GSEA is to determine whether the members of S are randomly distributed throughout the ranked gene list (L) or primarily found at the top or bottom.
There are three key elements of the GSEA method:
Calculation of an Enrichment Score.
The enrichment score (ES) represent the degree to which a set S is over-represented at the top or bottom of the ranked list L. The score is calculated by walking down the list L, increasing a running-sum statistic when we encounter a gene in S and decreasing when it is not. The magnitude of the increment depends on the gene statistics (e.g., correlation of the gene with phenotype). The ES is the maximum deviation from zero encountered in the random walk; it corresponds to a weighted Kolmogorov-Smirnov-like statistic (Subramanian et al. 2005).
Esimation of Significance Level of ES.
The p-value of the ES is calculated using permutation test. Specifically, we permute the gene labels of the gene list L and recompute the ES of the gene set for the permutated data, which generate a null distribution for the ES. The p-value of the observed ES is then calculated relative to this null distribution.
Adjustment for Multiple Hypothesis Testing.
When the entire gene sets were evaluated, DOSE adjust the estimated significance level to account for multiple hypothesis testing and also q-values were calculated for FDR control.
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma)
library(openxlsx)
library(gplots) #for heatmaps
library(DT) #interactive and searchable tables of our GSEA results
library(clusterProfiler) # provides a suite of tools for functional enrichment analysis
})
# Pick a pairwise comparison
yy <- 1
# Perform GSEA using clusterProfiler ----
# Which library to use for implementation? As per
# https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbz158/5722384:
# "For expression-based EA on the full expression matrix...When given raw read
# counts, we recommend to apply a VST such as voom [39] to arrive at
# library-size normalized logCPMs."
# For testing self-contained null hypothesis (test for association of any
# gene in the set with the phenotype), use ROAST
# For testing competitive null hypothesis (test for excess of differential
# expression in a gene set relative to genes outside the set) -
# **their recommendation**, use PADOG or SAFE?
#
# Ability to do this depends on the availability of gene sets. Major databases
# (e.g. msigdb don't seem to have stercoralis information. They do have
# C. elegans gene sets, but I'm not convinced the homology information is
# good enough for the comparison to be unbiased/meaningful.
#
# Although the Lok lab did GSEA in Ramanathan *et al* 2011 (PMID: 21572524),
# using C. elegans homologs are two manually compiled gene sets:
# 1) 31 genes with products known to be immunoreactive in
# *S. stercoralis*-infected humans
# 2) 42 putatively identified heat shock proteins
# Of course, this is before the genome was sequenced,
# so these genes don't have associated SSTP numbers.
#
# In Hunt et al 2016, there is an Ensembl Compara protein family set
# Note that this uses specific transcript information, which I throw out.
# (e.g. SSTP_0001137400.2 is recoded as SSTP_0001137400)
ensComp.geneIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 1) %>%
as_tibble() %>%
dplyr::select(-Family.members) %>%
pivot_longer(cols = -Compara.family.id, values_to = "geneID") %>%
dplyr::select(-name) %>%
dplyr::filter(grepl("SSTP_", geneID))
ensComp.geneIDs$geneID <- str_remove_all(ensComp.geneIDs$geneID, "\\.[0-9]$")
ensComp.geneIDs$geneID <- str_remove_all(ensComp.geneIDs$geneID, "[a-z]$")
# Compare these genes to the list of genes in our filtered, normalized list ----
#
compara.exclusive <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df, by = "geneID")
#nrow(compara.exclusive)
compara.absent <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df,., by = "geneID") %>%
dplyr::select(geneID)
#nrow(compara.absent)
# How many genes have associated GO terms? ----
GO.present <- list.myTopHits.df[[yy]]$GO_term %>%
gsub("NA", NA,.) %>%
as_tibble_col(column_name = "GO_Term") %>%
tibble(geneID = list.myTopHits.df[[yy]]$geneID,.) %>%
dplyr::filter(!is.na(GO_Term))
#nrow(GO.present)
# Are any of these genes part of those not found in the compara dataset? ----
GO.present.Compara.absent <- dplyr::semi_join(GO.present,
compara.absent,
by = "geneID")
#nrow(GO.present.Compara.absent)
# Make a list of genes
ensComp.familyIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 2,
cols = c(1,4:6)) %>%
as_tibble() %>%
dplyr::mutate(Family_Description = dplyr::coalesce(
.$Description,
.$`Top.product.(members.with.hit)`,
.$`Interpro.top.hit.(members.with.hit)`)
) %>%
dplyr::select(Compara.family.id, Family_Description)
ensComp <- left_join(ensComp.geneIDs,
ensComp.familyIDs,
by = "Compara.family.id") %>%
dplyr::select(-Compara.family.id) %>%
dplyr::rename(gs_name = Family_Description, gene_symbol = geneID) %>%
dplyr::relocate(gs_name, gene_symbol)
rm(ensComp.geneIDs, ensComp.familyIDs)
# Filter out genes that aren't part of our RNAseq dataset
genelist <- v.DEGList.filtered.norm$genes %>%
rownames_to_column(var = "geneID") %>%
dplyr::select(geneID)
ensComp<- ensComp %>%
dplyr::rename(geneID = gene_symbol) %>%
left_join(genelist, ., by = "geneID") %>%
dplyr::relocate(gs_name, geneID)
# Generate rank ordered list of genes ----
mydata.df.sub <- dplyr::select(list.myTopHits.df[[yy]], geneID, logFC)
mydata.gsea <- mydata.df.sub$logFC
names(mydata.gsea) <- as.character(mydata.df.sub$geneID)
mydata.gsea <- sort(mydata.gsea, decreasing = TRUE)
# run GSEA using the 'GSEA' function from clusterProfiler
# Given a priori defined set of gene S
# (e.g., genes shareing the same DO category),
# the goal of GSEA is to determine whether the members of S are
# randomly distributed throughout the ranked gene list (L) or
# primarily found at the top or bottom.
# There are three key elements of the GSEA method:
# **Calculation of an Enrichment Score.**
# The enrichment score (ES) represent the degree to which a set S is
# over-represented at the top or bottom of the ranked list L. The score is
# calculated by walking down the list L, increasing a running-sum statistic
# when we encounter a gene in S and decreasing when it is not. The magnitude
# of the increment depends on the gene statistics (e.g., correlation of the
# gene with phenotype). The ES is the maximum deviation from zero encountered
# in the random walk; it corresponds to a weighted Kolmogorov-Smirnov-like
# statistic (Subramanian et al. 2005).
# **Esimation of Significance Level of ES.**
# The p-value of the ES is calculated using permutation test. Specifically,
# we permute the gene labels of the gene list L and recompute the ES of the
# gene set for the permutated data, which generate a null distribution for the
# ES. The p-value of the observed ES is then calculated relative to this null
# distribution.
# **Adjustment for Multiple Hypothesis Testing.**
# When the entire gene sets were evaluated, DOSE adjust the estimated
# significance level to account for multiple hypothesis testing and also
# q-values were calculated for FDR control.
myGSEA.res <- GSEA(mydata.gsea, TERM2GENE=ensComp, verbose=FALSE)
## Warning in fgsea(pathways = geneSets, stats = geneList, nperm = nPerm, minSize = minGSSize, : There are ties in the preranked stats (0.28% of the list).
## The order of those tied genes will be arbitrary, which may produce unexpected results.
myGSEA.df <- as_tibble(myGSEA.res@result)
myGSEA.tbl<-as_tibble(myGSEA.res@result) %>%
dplyr::select(-c(Description, pvalue, enrichmentScore)) %>%
dplyr::rename(normalized_EnrichmentScore = NES)
# view results as an interactive table
enrichment.DT <- datatable(myGSEA.tbl,
rownames = TRUE,
caption = htmltools::tags$caption(
style =
'caption-side: top; text-align: left; color: black',
htmltools::tags$b(
'Gene Families Enriched in ',
gsub('-', ' vs ',
names(list.myTopHits.df)[[yy]]))
),
options = list(
autoWidth = TRUE,
scrollX = TRUE,
scrollY = '800px',
scrollCollapse = TRUE,
searchHighlight = TRUE,
order = list(3, 'desc'),
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
list(targets = "_all",
class="dt-right")))) %>%
formatRound(columns=c(3,5:6), digits=2) %>%
formatRound(columns=c(4), digits=4)
enrichment.DT
# add a variable to this result that matches enrichment direction with phenotype
myGSEA.df <- myGSEA.df %>%
mutate(life_stage = case_when(
NES > 0 ~ str_split(names(list.myTopHits.df)[[yy]],'-',simplify = T)[1,1],
NES < 0 ~ str_split(names(list.myTopHits.df)[[yy]],'-',simplify = T)[1,2]))
myGSEA.df$ID <- myGSEA.df$ID %>%
word(sep = ',') %>%
word(sep = ' and')
# create 'bubble plot' to summarize y signatures across x phenotypes
ggplot(myGSEA.df, aes(x=life_stage, y=ID)) +
geom_point(aes(size=setSize, color = NES, alpha=-log10(p.adjust))) +
scale_color_gradient(low="blue", high="red") +
labs(title = paste0('Gene Families Enriched in ',
gsub('-',' vs ',
names(list.myTopHits.df)[[yy]])),
subtitle = 'NES = Normalized Enrichment Score; Gene family assignments
from Ensembl Compara dataset defined in Hunt et al 2016',
x = "Life Stage",
y = "Family ID") +
theme_bw() +
theme(plot.title.position = "plot",
plot.caption.position = "plot",
plot.title = element_text(face = "bold",
size = 13, hjust = 0),
axis.title = element_text(face = "bold",size = 10.4),
legend.title = element_text(face="bold",size = 10.4),
aspect.ratio = 3/1)
load (file = "../Outputs/SsRNAseq_data_preprocessed")
targets <- SsRNAseq.preprocessed.data$targets
annotations <- SsRNAseq.preprocessed.data$annotations
log2.cpm.filtered.norm <- SsRNAseq.preprocessed.data$log2.cpm.filtered.norm
myDGEList.filtered.norm <-SsRNAseq.preprocessed.data$myDGEList.filtered.norm
rm(SsRNAseq.preprocessed.data)
load(file = "../Outputs/Ss_vDGEList")
# Check for presence of output plots folder, generate if it doesn't exist
output.path <- "../Outputs/Plots"
if (!dir.exists(output.path)){
dir.create(output.path)
}
# Introduction to this chunk -----------
# This code chunk starts with filtered and normalized abundance data in a data frame (not tidy).
# It will implement hierarchical clustering and PCA analyses on the data.
# It will plot various graphs and can save them in PDF files.
# Load packages ------
suppressPackageStartupMessages({
library(tidyverse) # you're familiar with this fromt the past two lectures
library(ggplot2)
library(RColorBrewer)
library(ggdendro)
library(magrittr)
library(factoextra)
library(gridExtra)
library(cowplot)
library(dendextend)
})
# Identify variables of interest in study design file ----
group <- factor(targets$group)
# Hierarchical clustering ---------------
# Remember: hierarchical clustering can only work on a data matrix, not a data frame
# Calculate distance matrix
# dist calculates distance between rows, so transpose data so that we get distance between samples.
# how similar are samples from each other
colnames(log2.cpm.filtered.norm)<-targets$group
distance <- dist(t(log2.cpm.filtered.norm), method = "maximum") #other distance methods are "euclidean", maximum", "manhattan", "canberra", "binary" or "minkowski"
# Calculate clusters to visualize differences. This is the hierarchical clustering.
# The methods here include: single (i.e. "friends-of-friends"), complete (i.e. complete linkage), and average (i.e. UPGMA). Here's a comparison of different types: https://en.wikipedia.org/wiki/UPGMA#Comparison_with_other_linkages
clusters <- hclust(distance, method = "complete") #other agglomeration methods are "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", or "centroid"
dend <- as.dendrogram(clusters)
p1<-dend %>%
dendextend::set("branches_k_color", k = 5) %>%
dendextend::set("hang_leaves", c(0.1)) %>%
dendextend::set("labels_cex", c(0.5)) %>%
dendextend::set("labels_colors", k = 5) %>%
dendextend::set("branches_lwd", c(0.7)) %>%
as.ggdend %>%
ggplot (offset_labels = -0.5) +
theme_dendro() +
ylim(0, max(get_branches_heights(dend))) +
labs(title = "Hierarchical Cluster Dendrogram ",
subtitle = "filtered, TMM normalized",
y = "Distance",
x = "Life stage") +
coord_fixed(1/2) +
theme(axis.title.x = element_text(color = "black"),
axis.title.y = element_text(angle = 90),
axis.text.y = element_text(angle = 0),
axis.line.y = element_line(color = "black"),
axis.ticks.y = element_line(color = "black"),
axis.ticks.length.y = unit(2, "mm"))
p1
# Principal component analysis (PCA) -------------
# this also works on a data matrix, not a data frame
pca.res <- prcomp(t(log2.cpm.filtered.norm), scale.=F, retx=T)
summary(pca.res) # Prints variance summary for all principal components.
#pca.res$rotation #$rotation shows you how much each gene influenced each PC (called 'scores')
#pca.res$x # 'x' shows you how much each sample influenced each PC (called 'loadings')
#note that these have a magnitude and a direction (this is the basis for making a PCA plot)
## This generates a screeplot: a standard way to view eigenvalues for each PCA. Shows the proportion of variance accounted for by each PC. Plotting only the first 10 dimensions.
p2<-fviz_eig(pca.res,
barcolor = brewer.pal(8,"Pastel2")[8],
barfill = brewer.pal(8,"Pastel2")[8],
linecolor = "black",
main = "Scree plot: proportion of variance accounted for by each principal component",
ggtheme = theme_bw())
p2
pc.var<-pca.res$sdev^2 # sdev^2 captures these eigenvalues from the PCA result
pc.per<-round(pc.var/sum(pc.var)*100, 1) # we can then use these eigenvalues to calculate the percentage variance explained by each PC
# Visualize the PCA result ------------------
#lets first plot any two PCs against each other
#We know how much each sample contributes to each PC (loadings), so let's plot
pca.res.df <- as_tibble(pca.res$x)
# Plotting PC1 and PC2
p3<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
fill = targets$group,
color = targets$group
) +
geom_point(size=4, shape= 21, color = "black", alpha = 0.5) +
scale_fill_brewer(palette = "Set2") +
scale_color_brewer(palette = "Set2", guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(title="Principal Components Analysis of S. stercoralis RNAseq Samples",
sub = "Note: analysis is blind to life stage identity.",
fill = "Life Stage") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10))
suppressMessages(ggsave("Ss_Multivariate_Plots_PCA.pdf",
plot = p3,
device = "pdf",
height = 4,
#width = 7,
path = output.path))
p3
# A guess at the identity of PC1
p4<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Maturity),
fill = factor(targets$Maturity)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")) +
scale_color_manual(values = brewer.pal(8,"Set2"), guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC1 identity: Adults vs Larvae",
fill = "Maturity") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
p4
# A guess at the identity of PC2
p5<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Infectious),
fill = factor(targets$Infectious)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")[3:4]) +
scale_color_manual(values = brewer.pal(8,"Set2")[3:4], guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC2 identity: Host-seeking/dwelling",
fill = 'Infectivity Stage') +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
p5
# Create a PCA 'small multiples' chart ----
pca.res.df <- pca.res$x[,1:6] %>%
as_tibble() %>%
add_column(sample = targets$sample,
group = group,
maturity = factor(targets$Maturity),
infectious = factor(targets$Infectious))
pca.pivot <- pivot_longer(pca.res.df, # dataframe to be pivoted
cols = PC1:PC3, # column names to be stored as a SINGLE variable
names_to = "PC", # name of that new variable (column)
values_to = "loadings") # name of new variable (column) storing all the values (data)
PC1<-subset(pca.pivot, PC == "PC1")
PC2 <-subset(pca.pivot, PC == "PC2")
PC3 <- subset(pca.pivot, PC == "PC3")
#PC4 <- subset(pca.pivot, PC == "PC4")
# New facet label names for PCs
PC.labs <- c(paste0("PC1 (",pc.per[1],"%",")"),
paste0("PC2 (",pc.per[2],"%",")"),
paste0("PC3 (",pc.per[3],"%",")")
)
names(PC.labs) <- c("PC1", "PC2", "PC3")
p6<-ggplot(pca.pivot) +
aes(x=sample, y=loadings) + # you could iteratively 'paint' different covariates onto this plot using the 'fill' aes
geom_bar(stat="identity", fill = brewer.pal(8,"Set2")[8]) +
scale_fill_brewer(palette = "Set2") +
facet_wrap(~PC, labeller = labeller(PC = PC.labs)) +
geom_bar(data = PC1, stat = "identity", aes(fill = group)) +
geom_bar(data = PC2, stat = "identity", aes(fill = group)) +
geom_bar(data = PC3, stat = "identity", aes(fill = group)) +
labs(title="S. stercoralis: PCA 'small multiples' plot",
fill = "Life Stage",
caption=paste0("produced on ", Sys.time())) +
scale_x_discrete(limits = targets$sample, labels = targets$sample) +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10)) +
coord_flip()
suppressMessages(ggsave("Ss_Multivariate_Plots_Small_Multiples.pdf",
plot = p6,
device = "pdf",
height = 4,
width = 8,
path = output.path))
p6
# Introduction to this chunk ----
# This chunk provides additional analysis of the principal components, in order to determine which genes are influencing the identified PCs.
# Use pca.res$rotation to select genes influencing PC1-6 ----
myscores.df <- pca.res$rotation[,1:6] %>%
as_tibble(rownames = "geneID") %>%
pivot_longer(cols = -geneID, names_to = "PC", values_to = "scores") %>%
dplyr::mutate(abs_scores = abs(scores)) %>%
group_by(PC) %>%
slice_max(abs_scores, prop = .1) # get top 10% of genes in all PCs
# Pull out genes that are the top 10% of contributors (in any direction) to PC1 and PC2. Annotate.
myscores.Top10 <- myscores.df %>%
dplyr::filter(PC == "PC1" | PC == "PC2") %>%
pivot_wider(id_cols = geneID,
names_from = PC,
values_from = scores) %>%
dplyr::mutate(PC1_id = case_when(PC1 > 0 ~ "larval",
PC1 < 0 ~ "adult",
is.na(PC1) ~ "--")) %>%
dplyr::mutate(PC2_id = case_when(PC2 > 0 ~ "parasite",
PC2 < 0 ~ "non_parasite",
is.na(PC2) ~ "--")) %>%
dplyr::left_join(.,(rownames_to_column(annotations, var = "geneID")), by = "geneID") %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term, Ce_geneID, Ce_percent_homology, .after = PC2_id)
# Make Interactive Plot
myscores.Top10.interactive <- myscores.Top10 %>%
DT::datatable(extensions = c('KeyTable', "FixedHeader"),
rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left;',
htmltools::tags$b('Top 10% of Genes Contributing to PC1 and PC2'),
htmltools::tags$br(),
'Search for PC1_id/PC2_id values to filter results'),
options = list(keys = TRUE,
autoWidth = TRUE,
scrollX = TRUE,
scrollY = '300px',
order = list(1, 'desc'),
searchHighlight = TRUE,
pageLength = 10,
lengthMenu = c("10", "25", "50", "100"))) %>%
DT::formatRound(columns=c(2:3), digits=3)
myscores.Top10.interactive
suppressPackageStartupMessages({
library(pheatmap)
library(RColorBrewer)
library(heatmaply)
})
diffGenes <- v.DEGList.filtered.norm$E %>%
as_tibble(rownames = "geneID", .name_repair = "unique") %>%
dplyr::select(!geneID) %>%
as.matrix()
rownames(diffGenes) <- rownames(v.DEGList.filtered.norm$E)
colnames(diffGenes) <- as.character(v.DEGList.filtered.norm$targets$samples)
clustColumns <- hclust(as.dist(1-cor(diffGenes, method="spearman")), method="complete")
clustRows <- hclust(as.dist(1-cor(t(diffGenes),
method="pearson")),
method="complete")
par(cex.main=0.5)
showticklabels <- c(TRUE,FALSE)
p<-pheatmap(diffGenes,
color = RdBu(75),
cluster_rows = clustRows,
cluster_cols = clustColumns,
show_rownames = F,
scale = "row",
angle_col = 45,
main = "Ss: Log2 Counts Per Million (CPM) Expression Across Life Stages"
)
# suppressMessages(ggsave('Ss_Log2CPM_Heatmap.png',
# plot = p,
# width = 6,
# height = 4,
# device = "png",
# #useDingbats=FALSE,
# path = output.path))
#
# p
# Introduction to this chunk ----
# This chunk uses a variance-stabilized DGEList of filtered and normalized abundance data.
#
# These data/results are examples, a responsive version of this code is avaliable in a Shiny App.
#
# Because we have access to biological replicates, we can use statistical tools for differential expression analysis
# Useful reading on differential expression: https://ucdavis-bioinformatics-training.github.io/2018-June-RNA-Seq-Workshop/thursday/DE.html
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma) # differential gene expression using linear modeling
library(edgeR)
library(gt)
library(DT)
library(plotly)
library(ggthemes)
library(RColorBrewer)
source("./theme_Publication.R")
})
diffGenes.df <- v.DEGList.filtered.norm$E %>%
as_tibble(rownames = "geneID", .name_repair = "unique")
# Set Expression threshold values for plotting and saving DEGs ----
adj.P.thresh <- 0.05
lfc.thresh <- 1
group <- factor(targets$group)
design <- model.matrix(~0 + group) # no intercept/blocking for matrix, comparisons across group
colnames(design) <- levels(group)
# Fit a linear model to the data ----
fit <- lmFit(v.DEGList.filtered.norm, design)
# As an example, generate comparison matrix for a pairwise comparison ----
# iL3s vs FLF
# Note that the target/contrast goups will be divided by the number of life
# stage groups e.g. PF+FLF/2 - iL3+iL3a+pfL1+ppL1+ppL3/5
comparison <- c('(iL3)-(FLF)')
targetStage<- comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,1] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
contrastStage<-comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,2] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
comparison<- sapply(seq_along(comparison),function(x){
tS <- as.vector(targetStage[x,]) %>%
.[. != ""]
cS <- as.vector(contrastStage[x,]) %>%
.[. != ""]
paste(paste0(tS,
collapse = "+") %>%
paste0("(",.,")/",length(tS)),
paste0(cS,
collapse = "+") %>%
paste0("(",.,")/",length(cS)),
sep = "-")
})
# Generate contrast matrix ----
contrast.matrix <- makeContrasts(contrasts = comparison,
levels=design)
# extract the linear model fit -----
fits <- contrasts.fit(fit, contrast.matrix)
# empirical bayes smoothing of gene-wise standard deviations provides increased power (see: https://www.degruyter.com/doi/10.2202/1544-6115.1027)
ebFit <- eBayes(fits)
# Pull out the DEGs that pass a specific threshold for all pairwise comparisons ----
# Adjust for multiple comparisons using method = global.
results <- decideTests(ebFit, method="global", adjust.method="BH", p.value = adj.P.thresh)
recode01<- function(x){
case_when(x == 1 ~ "Up",
x == -1 ~ "Down",
x == 0 ~ "NotSig")
}
diffDesc <- results %>%
as_tibble(rownames = "geneID") %>%
dplyr::mutate(across(-geneID, unclass)) %>%
dplyr::mutate(across(where(is.double), recode01))
# Function that identifies top DEGs between a specific contrast ----
calc_DEG_tbl <- function (ebFit, coef) {
myTopHits.df <- limma::topTable(ebFit, adjust ="BH",
coef=coef, number=40000,
sort.by="logFC") %>%
as_tibble(rownames = "geneID") %>%
dplyr::rename(tStatistic = t, LogOdds = B, BH.adj.P.Val = adj.P.Val) %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term,
In.subclade_geneID, In.subclade_percent_homology,
Out.subclade_geneID, Out.subclade_percent_homology,
Ce_geneID, Ce_percent_homology, .after = LogOdds)
myTopHits.df
}
list.myTopHits.df <- sapply(comparison, function(y){
calc_DEG_tbl(ebFit, y)},
simplify = FALSE,
USE.NAMES = TRUE)
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::select(geneID,
logFC,
BH.adj.P.Val:Ce_percent_homology)},
simplify = FALSE,
USE.NAMES = TRUE)
# Get log2CPM values and threshold information for genes of interest
list.myTopHits.df <- sapply(seq_along(comparison), function(y){
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
concat_name <- function(x) {
ifelse(x == "target",
paste(tS, collapse = "+"),
paste(cS, collapse = "+"))
}
groupAvgs <- diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
pivot_longer(cols = -geneID, names_to = c("group","sample"), values_to = "CPM",
names_sep = "-") %>%
dplyr::mutate(contrastID = if_else(group %in% tS,"target", "contrast")) %>%
group_by(geneID, contrastID) %>%
dplyr::select(-sample) %>%
summarize(mean = mean(CPM), .groups = "drop_last") %>%
pivot_wider(names_from = contrastID, values_from = mean) %>%
dplyr::relocate(contrast, .after = target) %>%
dplyr::rename_with(concat_name, -geneID) %>%
dplyr::rename_with(.cols =-geneID, .fn = ~ paste0("avg_(",.x,")"))
diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
left_join(groupAvgs, by = "geneID") %>%
left_join(list.myTopHits.df[[y]],., by = "geneID") %>%
left_join(dplyr::select(diffDesc,geneID,comparison[y]), by = "geneID") %>%
dplyr::rename(DEG_Desc=comparison[y]) %>%
dplyr::relocate(DEG_Desc) %>%
dplyr::relocate(logFC:Ce_percent_homology, .after = last_col())
},
simplify = FALSE)
comparison <- gsub("/[0-9]*","", comparison)
names(list.myTopHits.df) <- comparison
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::mutate(DEG_Desc = case_when(DEG_Desc == "Up" ~ paste0("Up in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "Down" ~ paste0("Down in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "NotSig" ~ "NotSig"))
},
simplify = FALSE,
USE.NAMES = TRUE)
# PC1 Volcano Plot and Interactive Table ----
vplot1 <- ggplot(list.myTopHits.df[[1]]) +
aes(y=-log10(BH.adj.P.Val), x=logFC, text = paste(geneID, "<br>",
"logFC:", round(logFC, digits = 2), "<br>",
"p-val:", format(BH.adj.P.Val, digits = 3, scientific = TRUE))) +
geom_point(size=2) +
geom_hline(yintercept = -log10(adj.P.thresh),
linetype="longdash",
colour="grey",
size=1) +
geom_vline(xintercept = lfc.thresh,
linetype="longdash",
colour="#BE684D",
size=1) +
geom_vline(xintercept = -lfc.thresh,
linetype="longdash",
colour="#2C467A",
size=1) +
labs(title = paste0('Pairwise Comparison: ',
gsub('-',
' vs ',
comparison[1])),
subtitle = paste0("grey line: p = ",
adj.P.thresh, "; colored lines: log-fold change = ", lfc.thresh),
color = "GeneIDs") +
theme_Publication()
vplot1
# Interactive Tables
y<- 1
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
sample.num.tS <- sapply(tS, function(x) {colSums(v.DEGList.filtered.norm$design)[[x]]}) %>% sum()
sample.num.cS <- sapply(cS, function(x) {colSums(v.DEGList.filtered.norm$design)[[x]]}) %>% sum()
n_num_cols <- sample.num.tS + sample.num.cS + 5
index_homologs <- length(colnames(list.myTopHits.df[[y]])) - 5
LS.datatable <- list.myTopHits.df[[y]] %>%
DT::datatable(rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left; color: black',
htmltools::tags$b('Differentially Expressed Genes in',
htmltools::tags$em('S. stercoralis'),
gsub('-',' vs ',comparison[y])),
htmltools::tags$br(),
"Threshold: p < ",
adj.P.thresh, "; log-fold change > ",
lfc.thresh,
htmltools::tags$br(),
'Values = log2 counts per million'),
options = list(autoWidth = TRUE,
scrollX = TRUE,
scrollY = '300px',
scrollCollapse = TRUE,
order = list(n_num_cols-1,
'desc'),
searchHighlight = TRUE,
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
list(
targets = ((n_num_cols +
1)),
render = JS(
"function(data, row) {",
"data.toExponential(1);",
"}")
),
list(
targets = ((n_num_cols +
4):(n_num_cols +
5)),
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 20 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 20) + '...</span>' : data;",
"}")
),
list(targets = "_all",
class="dt-right")
),
rowCallback = JS(c(
"function(row, data){",
" for(var i=0; i<data.length; i++){",
" if(data[i] === null){",
" $('td:eq('+i+')', row).html('NA')",
" .css({'color': 'rgb(151,151,151)', 'font-style': 'italic'});",
" }",
" }",
"}"
))
))
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(3:n_num_cols),
digits=3)
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(n_num_cols+2,
index_homologs+1,
index_homologs+3),
digits=2)
LS.datatable <- LS.datatable %>%
DT::formatSignif(columns=c(n_num_cols+1),
digits=3)
LS.datatable
suppressPackageStartupMessages({
library(openxlsx)
library(tidyverse)
library(ggplot2)
})
# Load Hunt Dataset: iL3 vs FLF comparison
temp.dat <- read.xlsx ("../Benchmarking/ng.3495-S5.xlsx",
sheet = 5, startRow = 2)
Hunt.dat <- tibble(geneID = temp.dat$X2, logFC = temp.dat$logFC)
rm(temp.dat)
# Rename Results of iL3 vs FLF comparison from Browser
Browser.dat <- list.myTopHits.df$`(iL3)-(FLF)` %>%
dplyr::select(geneID, logFC)
print(paste('Total number of genes in Hunt *et al* 2016 iL3 vs FLF comparison tab:',nrow(Hunt.dat)))
print(paste('Total number of genes in Str-RNAseq Browser iL3 vs FLF output file:', nrow(Browser.dat)))
# The plot below takes the genes with LogFC results in both the Browser and Hunt databases, and plots the two sets against each other.
plotting.all <- inner_join(Browser.dat, Hunt.dat, by = "geneID")
p.benchmark <- ggplot(plotting.all, aes(x = logFC.x, y = logFC.y)) +
geom_smooth(method=lm, color = 'red', formula = "y ~ x") +
geom_point(shape=16, size=3, alpha = 0.8) +
labs(title = "S. stercoralis: Str-Browser vs Hunt Data",
subtitle = "iL3 vs FLF",
caption = "points = genes; red line = linear model (y ~ x)",
x = "Str-Browser LogFC",
y = "Hunt LogFC") +
coord_equal() +
theme_bw() +
theme(text = element_text(size = 10),
title = element_text(size = 10))
suppressMessages(ggsave("Ss_Benchmarking.pdf",
plot = p.benchmark,
device = "pdf",
height = 4,
#width = 8,
path = output.path))
p.benchmark
# Introduction to this chunk ----
# this chunk creates heatmaps from differentially expressed genes;
# it takes as input a list of genes that are differentially expressed in any life stage
# It selects modules of co-expressed genes based on pearson correlations
#
# These data/results are examples of possible analyses that can be run on this data.
# Load packages -----
suppressPackageStartupMessages({
library(tidyverse)
library(limma)
library(RColorBrewer)
library(gplots)
library(heatmaply)
library(ggplot2)
library(egg)
library(dendextend)
source("./ggheatmap_local.R")
})
# Choose a color pallette ----
myheatcolors <- RdBu(75)
# Select the comparison
y = 1
# Generate variable containing expression data for the thresholded DEGs
diffGenes.thresh <- v.DEGList.filtered.norm$E[results[,y] !=0,]
colnames(diffGenes.thresh) <- paste0(targets$group, "-", targets$sample)
# Cluster DEGs across stages ----
#begin by clustering the genes (rows) for a list of genes that are differentially expressed in at least one life stage
# use the 'cor' function and the pearson method for finding all pairwise correlations of genes
# '1-cor' converts this to a 0-2 scale for each of these correlations, which can then be used to calculate a distance matrix using 'as.dist'
clustRows <- hclust(as.dist(1-cor(t(diffGenes.thresh), method="pearson")), method="complete")
# hierarchical clustering is a type of unsupervised clustering.
# NOTE: this cluster may provide different results to one based on log2.cpm.filtered.norm data, likely b/c this version is specifcally focused on genes that are significantly different between conditions.
# Related methods include K-means, SOM, etc
# unsupervised methods are blind to sample/group identity
# in contrast, supervised methods 'train' on a set of labeled data.
# supervised clustering methods include random forest, and artificial neural networks
# cluster samples (columns)
# cluster columns by spearman correlation
clustColumns <- hclust(as.dist(1-cor(diffGenes.thresh,
method="spearman")),
method="complete")
#note: use Spearman, instead of Pearson, for clustering samples because it
#gives equal weight to highly vs lowly expressed transcripts or genes
#Cut the resulting tree and create color vector for clusters.
module.assign <- stats::cutree(clustRows, k=14) #The diffGenes info is
based on a pairwise comparison between all 7 life stages.
# assign a color to each module (makes it easy to identify and manipulate)
module.color <- rainbow(length(unique(module.assign)), start=0.1, end=0.9)
module.color <- module.color[as.vector(module.assign)]
# # simplfy heatmap by averaging the biological replicates
# and display only one column per condition
# diffGenes.AVG <- avearrays(diffGenes.thresh)
# plot the hclust results as a heatmap, grouping the life stages together
diffGenes.heatmap <- heatmap.2(diffGenes.thresh,
srtCol = 0, adjCol= c(0.5,0.5),
Rowv=as.dendrogram(clustRows),
Colv=as.dendrogram(clustColumns),
key.title = NA,
main = paste0("DEG Heatmap (by life stage): "),
sub = paste0("Genes pass threshold in >= 1 comparison. Threshold: p < ",
adj.P.thresh, "; log-fold change > ",
lfc.thresh),
RowSideColors=module.color,
col=rev(myheatcolors), scale='row', labRow=NA,
density.info="none", trace="none",
cexRow=1, cexCol=1)
## GGPlots version
# gg.diffGenes.heatmap<-ggheatmap_local(diffGenes.thresh,
# colors = rev(myheatcolors),
# Rowv= ladderize(as.dendrogram(clustRows)),
# Colv=ladderize(as.dendrogram(clustColumns)),
# key.title = "Log2CPM",
# branches_lwd = 0.2,
# showticklabels = c(TRUE, FALSE),
# scale='row',
# cexRow=1, cexCol=1)
# suppressMessages(ggsave("./heatmap.pdf", plot = gg.heatmap, width = 11, height = 8, units = "in", device = "pdf"))
# Make an interactive version
# interactive.diffGenes.heatmap <- heatmaply(diffGenes.thresh,
# colors = rev(myheatcolors),
# Rowv= ladderize(as.dendrogram(clustRows)),
# Colv=ladderize(as.dendrogram(clustColumns)),
# showticklabels = c(TRUE, FALSE),
# scale='row',
# plot_method = "ggplot",
# branches_lwd = 0.2,
# key.title = "Log2CPM",
# cexRow=1, cexCol=1)
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma)
library(openxlsx)
library(gplots) #for heatmaps
library(DT) #interactive and searchable tables of our GSEA results
library(clusterProfiler) # provides a suite of tools for functional enrichment analysis
})
# Pick a pairwise comparison
yy <- 1
# Perform GSEA using clusterProfiler ----
# Which library to use for implementation? As per
# https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbz158/5722384:
# "For expression-based EA on the full expression matrix...When given raw read
# counts, we recommend to apply a VST such as voom [39] to arrive at
# library-size normalized logCPMs."
# For testing self-contained null hypothesis (test for association of any
# gene in the set with the phenotype), use ROAST
# For testing competitive null hypothesis (test for excess of differential
# expression in a gene set relative to genes outside the set) -
# **their recommendation**, use PADOG or SAFE?
#
# Ability to do this depends on the availability of gene sets. Major databases
# (e.g. msigdb don't seem to have stercoralis information. They do have
# C. elegans gene sets, but I'm not convinced the homology information is
# good enough for the comparison to be unbiased/meaningful.
#
# Although the Lok lab did GSEA in Ramanathan *et al* 2011 (PMID: 21572524),
# using C. elegans homologs are two manually compiled gene sets:
# 1) 31 genes with products known to be immunoreactive in
# *S. stercoralis*-infected humans
# 2) 42 putatively identified heat shock proteins
# Of course, this is before the genome was sequenced,
# so these genes don't have associated SSTP numbers.
#
# In Hunt et al 2016, there is an Ensembl Compara protein family set
# Note that this uses specific transcript information, which I throw out.
# (e.g. SSTP_0001137400.2 is recoded as SSTP_0001137400)
ensComp.geneIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 1) %>%
as_tibble() %>%
dplyr::select(-Family.members) %>%
pivot_longer(cols = -Compara.family.id, values_to = "geneID") %>%
dplyr::select(-name) %>%
dplyr::filter(grepl("SSTP_", geneID))
ensComp.geneIDs$geneID <- str_remove_all(ensComp.geneIDs$geneID, "\\.[0-9]$")
ensComp.geneIDs$geneID <- str_remove_all(ensComp.geneIDs$geneID, "[a-z]$")
# Compare these genes to the list of genes in our filtered, normalized list ----
#
compara.exclusive <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df, by = "geneID")
#nrow(compara.exclusive)
compara.absent <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df,., by = "geneID") %>%
dplyr::select(geneID)
#nrow(compara.absent)
# How many genes have associated GO terms? ----
GO.present <- list.myTopHits.df[[yy]]$GO_term %>%
gsub("NA", NA,.) %>%
as_tibble_col(column_name = "GO_Term") %>%
tibble(geneID = list.myTopHits.df[[yy]]$geneID,.) %>%
dplyr::filter(!is.na(GO_Term))
#nrow(GO.present)
# Are any of these genes part of those not found in the compara dataset? ----
GO.present.Compara.absent <- dplyr::semi_join(GO.present,
compara.absent,
by = "geneID")
#nrow(GO.present.Compara.absent)
# Make a list of genes
ensComp.familyIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 2,
cols = c(1,4:6)) %>%
as_tibble() %>%
dplyr::mutate(Family_Description = dplyr::coalesce(
.$Description,
.$`Top.product.(members.with.hit)`,
.$`Interpro.top.hit.(members.with.hit)`)
) %>%
dplyr::select(Compara.family.id, Family_Description)
ensComp <- left_join(ensComp.geneIDs,
ensComp.familyIDs,
by = "Compara.family.id") %>%
dplyr::select(-Compara.family.id) %>%
dplyr::rename(gs_name = Family_Description, gene_symbol = geneID) %>%
dplyr::relocate(gs_name, gene_symbol)
rm(ensComp.geneIDs, ensComp.familyIDs)
# Filter out genes that aren't part of our RNAseq dataset
genelist <- v.DEGList.filtered.norm$genes %>%
rownames_to_column(var = "geneID") %>%
dplyr::select(geneID)
ensComp<- ensComp %>%
dplyr::rename(geneID = gene_symbol) %>%
left_join(genelist, ., by = "geneID") %>%
dplyr::relocate(gs_name, geneID)
# Generate rank ordered list of genes ----
mydata.df.sub <- dplyr::select(list.myTopHits.df[[yy]], geneID, logFC)
mydata.gsea <- mydata.df.sub$logFC
names(mydata.gsea) <- as.character(mydata.df.sub$geneID)
mydata.gsea <- sort(mydata.gsea, decreasing = TRUE)
# run GSEA using the 'GSEA' function from clusterProfiler
# Given a priori defined set of gene S
# (e.g., genes shareing the same DO category),
# the goal of GSEA is to determine whether the members of S are
# randomly distributed throughout the ranked gene list (L) or
# primarily found at the top or bottom.
# There are three key elements of the GSEA method:
# **Calculation of an Enrichment Score.**
# The enrichment score (ES) represent the degree to which a set S is
# over-represented at the top or bottom of the ranked list L. The score is
# calculated by walking down the list L, increasing a running-sum statistic
# when we encounter a gene in S and decreasing when it is not. The magnitude
# of the increment depends on the gene statistics (e.g., correlation of the
# gene with phenotype). The ES is the maximum deviation from zero encountered
# in the random walk; it corresponds to a weighted Kolmogorov-Smirnov-like
# statistic (Subramanian et al. 2005).
# **Esimation of Significance Level of ES.**
# The p-value of the ES is calculated using permutation test. Specifically,
# we permute the gene labels of the gene list L and recompute the ES of the
# gene set for the permutated data, which generate a null distribution for the
# ES. The p-value of the observed ES is then calculated relative to this null
# distribution.
# **Adjustment for Multiple Hypothesis Testing.**
# When the entire gene sets were evaluated, DOSE adjust the estimated
# significance level to account for multiple hypothesis testing and also
# q-values were calculated for FDR control.
myGSEA.res <- GSEA(mydata.gsea, TERM2GENE=ensComp, verbose=FALSE)
myGSEA.df <- as_tibble(myGSEA.res@result)
myGSEA.tbl<-as_tibble(myGSEA.res@result) %>%
dplyr::select(-c(Description, pvalue, enrichmentScore)) %>%
dplyr::rename(normalized_EnrichmentScore = NES)
# view results as an interactive table
enrichment.DT <- datatable(myGSEA.tbl,
rownames = TRUE,
caption = htmltools::tags$caption(
style =
'caption-side: top; text-align: left; color: black',
htmltools::tags$b(
'Gene Families Enriched in ',
gsub('-', ' vs ',
names(list.myTopHits.df)[[yy]]))
),
options = list(
autoWidth = TRUE,
scrollX = TRUE,
scrollY = '800px',
scrollCollapse = TRUE,
searchHighlight = TRUE,
order = list(3, 'desc'),
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
list(targets = "_all",
class="dt-right")))) %>%
formatRound(columns=c(3,5:6), digits=2) %>%
formatRound(columns=c(4), digits=4)
enrichment.DT
# add a variable to this result that matches enrichment direction with phenotype
myGSEA.df <- myGSEA.df %>%
mutate(life_stage = case_when(
NES > 0 ~ str_split(names(list.myTopHits.df)[[yy]],'-',simplify = T)[1,1],
NES < 0 ~ str_split(names(list.myTopHits.df)[[yy]],'-',simplify = T)[1,2]))
myGSEA.df$ID <- myGSEA.df$ID %>%
word(sep = ',') %>%
word(sep = ' and')
# create 'bubble plot' to summarize y signatures across x phenotypes
ggplot(myGSEA.df, aes(x=life_stage, y=ID)) +
geom_point(aes(size=setSize, color = NES, alpha=-log10(p.adjust))) +
scale_color_gradient(low="blue", high="red") +
labs(title = paste0('Gene Families Enriched in ',
gsub('-',' vs ',
names(list.myTopHits.df)[[yy]])),
subtitle = 'NES = Normalized Enrichment Score; Gene family assignments
from Ensembl Compara dataset defined in Hunt et al 2016',
x = "Life Stage",
y = "Family ID") +
theme_bw() +
theme(plot.title.position = "plot",
plot.caption.position = "plot",
plot.title = element_text(face = "bold",
size = 13, hjust = 0),
axis.title = element_text(face = "bold",size = 10.4),
legend.title = element_text(face="bold",size = 10.4),
aspect.ratio = 3/1)
sessionInfo()
sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Catalina 10.15.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] clusterProfiler_3.14.3 gplots_3.0.4 openxlsx_4.1.5
## [4] ggthemes_4.2.0 DT_0.14 gt_0.2.1
## [7] edgeR_3.28.1 limma_3.42.2 heatmaply_1.1.1
## [10] viridis_0.5.1 viridisLite_0.3.0 plotly_4.9.2.9000
## [13] pheatmap_1.0.12 dendextend_1.13.4 cowplot_1.0.0
## [16] gridExtra_2.3 factoextra_1.0.7 magrittr_1.5
## [19] ggdendro_0.1-20 RColorBrewer_1.1-2 forcats_0.5.0
## [22] stringr_1.4.0 dplyr_1.0.1 purrr_0.3.4
## [25] readr_1.3.1 tidyr_1.1.1 tibble_3.0.3
## [28] ggplot2_3.3.2 tidyverse_1.3.0
##
## loaded via a namespace (and not attached):
## [1] readxl_1.3.1 backports_1.1.8 fastmatch_1.1-0
## [4] igraph_1.2.5 plyr_1.8.6 lazyeval_0.2.2
## [7] splines_3.6.3 BiocParallel_1.20.1 crosstalk_1.1.0.1
## [10] urltools_1.7.3 digest_0.6.25 foreach_1.5.0
## [13] htmltools_0.5.0 GOSemSim_2.12.1 GO.db_3.10.0
## [16] gdata_2.18.0 fansi_0.4.1 memoise_1.1.0
## [19] cluster_2.1.0 gclus_1.3.2 graphlayouts_0.7.0
## [22] modelr_0.1.8 prettyunits_1.1.1 enrichplot_1.6.1
## [25] colorspace_1.4-1 blob_1.2.1 rvest_0.3.5
## [28] ggrepel_0.8.2 haven_2.3.1 xfun_0.15
## [31] crayon_1.3.4 jsonlite_1.7.0 iterators_1.0.12
## [34] glue_1.4.1 polyclip_1.10-0 registry_0.5-1
## [37] gtable_0.3.0 webshot_0.5.2 car_3.0-8
## [40] BiocGenerics_0.32.0 abind_1.4-5 scales_1.1.1
## [43] DOSE_3.12.0 DBI_1.1.0 rstatix_0.6.0
## [46] Rcpp_1.0.5 progress_1.2.2 gridGraphics_0.5-0
## [49] europepmc_0.4 foreign_0.8-76 bit_1.1-15.2
## [52] stats4_3.6.3 htmlwidgets_1.5.1.9001 httr_1.4.2
## [55] fgsea_1.12.0 ellipsis_0.3.1 pkgconfig_2.0.3
## [58] farver_2.0.3 dbplyr_1.4.4 locfit_1.5-9.4
## [61] ggplotify_0.0.5 tidyselect_1.1.0 labeling_0.3
## [64] rlang_0.4.7 reshape2_1.4.4 AnnotationDbi_1.48.0
## [67] munsell_0.5.0 cellranger_1.1.0 tools_3.6.3
## [70] cli_2.0.2 generics_0.0.2 RSQLite_2.2.0
## [73] ggridges_0.5.2 broom_0.5.6 evaluate_0.14
## [76] yaml_2.2.1 knitr_1.29 bit64_0.9-7
## [79] fs_1.4.2 tidygraph_1.2.0 zip_2.0.4
## [82] caTools_1.18.0 ggraph_2.0.3 nlme_3.1-148
## [85] DO.db_2.9 xml2_1.3.2 compiler_3.6.3
## [88] rstudioapi_0.11 curl_4.3 ggsignif_0.6.0
## [91] reprex_0.3.0 tweenr_1.0.1 stringi_1.4.6
## [94] lattice_0.20-41 Matrix_1.2-18 vctrs_0.3.2
## [97] pillar_1.4.6 lifecycle_0.2.0 BiocManager_1.30.10
## [100] triebeard_0.3.0 data.table_1.12.8 bitops_1.0-6
## [103] seriation_1.2-8 qvalue_2.18.0 R6_2.4.1
## [106] TSP_1.1-10 KernSmooth_2.23-17 rio_0.5.16
## [109] IRanges_2.20.2 codetools_0.2-16 MASS_7.3-51.6
## [112] gtools_3.8.2 assertthat_0.2.1 withr_2.2.0
## [115] S4Vectors_0.24.4 mgcv_1.8-31 parallel_3.6.3
## [118] hms_0.5.3 grid_3.6.3 rvcheck_0.1.8
## [121] rmarkdown_2.3 carData_3.0-4 ggpubr_0.4.0
## [124] ggforce_0.3.2 Biobase_2.46.0 lubridate_1.7.9